package com.fortune.service.impl;

import com.fortune.dao.DEmptyStorageDao;
import com.fortune.dto.in.dEmptyStorage.DEmptyStorageDto;
import com.fortune.dto.in.dStoreOut.DStoreOutDetailDto;
import com.fortune.entity.DUserInfo;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.DEmptyStorageService;
import com.fortune.vo.dEmptyStorage.AllDetail;
import com.fortune.vo.dEmptyStorage.DEmptyDetail;
import com.fortune.vo.dEmptyStorage.DEmptyStorageList;
import com.fortune.vo.dStoreIn.StorageDetails;
import com.fortune.vo.dStoreOut.DstoreId;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/20 0020.
 */
@Service
public class DEmptyStorageServiceImpl implements DEmptyStorageService {
    @Autowired
    private DEmptyStorageDao dEmptyStorageDao;
    @Override
    public List<DEmptyStorageList> findEmptyStorageList(DEmptyStorageDto dEmptyStorageDto, String did) {
        List<DEmptyStorageList> list = new ArrayList<DEmptyStorageList>();
        List<DEmptyStorageList> listOrder = dEmptyStorageDao.findClientOrderList(dEmptyStorageDto,did);
        for (int i =0; i<listOrder.size(); i++){
            int index = 0;
            DEmptyStorageList dEmptyStorageListOrder = listOrder.get(i);
            if(dEmptyStorageListOrder.getClassification().equals("订货")){
                List<AllDetail> list1 = dEmptyStorageDao.findDetail1(dEmptyStorageListOrder.getId(),did);
                for(AllDetail allDetail : list1){
                    if(allDetail.getBackBoxNum() == 0){
                        index = index+1;
                    }
                }
                if(list1.size() == index){
                    listOrder.remove(i);
                }
            }else{
                List<AllDetail> list1 = dEmptyStorageDao.findDetail2(dEmptyStorageListOrder.getId(),did);
                for(AllDetail allDetail : list1){
                    if(allDetail.getBackBoxNum() == 0){
                        index = index+1;
                    }
                }
                if(list1.size() == index){
                    listOrder.remove(i);
                }
            }
            list.add(dEmptyStorageListOrder);
        }
        return list;
    }

    @Override
    public List<DEmptyStorageList> findEmptyStorageLists(DEmptyStorageDto dEmptyStorageDto, String did) {
        return dEmptyStorageDao.findClientOrderLists(dEmptyStorageDto,did);
    }

    @Override
    public int findEmptyStorageListCount(DEmptyStorageDto dEmptyStorageDto, String did) {
        int a = dEmptyStorageDao.findClientOrderCount(dEmptyStorageDto,did);
        int b = dEmptyStorageDao.findReturnDepositCount(dEmptyStorageDto,did);
        return a+b;
    }

    @Override
    public List<DEmptyDetail> findListDetail(DStoreOutDetailDto dStoreOutDetailDto, String did) {
        List<DEmptyDetail> lists = new ArrayList<>();
        List<DstoreId> list = dStoreOutDetailDto.getId();
        for (int i =0; i<list.size(); i++){
            DstoreId dstoreId = list.get(i);
            String type = dstoreId.getType();
            if(type.equals("订货")){
                List<DEmptyDetail> aa = dEmptyStorageDao.findListOrderDetail(dstoreId.getId(),did);
                for (DEmptyDetail dEmptyDetail:aa){
                    if(!dEmptyDetail.getNum().equals("0")) {
                        lists.add(dEmptyDetail);
                    }
                }
            }else if(type.equals("退押金")){
                List<DEmptyDetail> aa = dEmptyStorageDao.findListDespsitDetail(dstoreId.getId(),did);
                for (DEmptyDetail dEmptyDetail:aa){
                    if(!dEmptyDetail.getNum().equals("0")) {
                        lists.add(dEmptyDetail);
                    }
                }
            }

        };
        return lists;
    }

    @Override
    @Transactional
    public int updateDConfirmBox(DUserInfo dUserInfo, DStoreOutDetailDto dStoreOutDetailDto) {
        List<DstoreId> lists= dStoreOutDetailDto.getId();
        if(lists.size()<0){
            return -2;
        }
        for (DstoreId dstoreId : lists){
            if(dstoreId.getType().equals("订货")){
                int num = dEmptyStorageDao.updateOrderType(dUserInfo.getdId(),dUserInfo.getId(),dstoreId.getId());
                if (num <= 0){
                    throw new UpdateInnerErrorException("确定回空箱失败");
                }
            }else if(dstoreId.getType().equals("退押金")){
                int num = dEmptyStorageDao.updateDespsitType(dUserInfo.getdId(),dUserInfo.getId(),dstoreId.getId());
                if (num <= 0){
                    throw new UpdateInnerErrorException("确定回空箱失败");
                }
            }
        }
        return 1;
    }
}
